package com.elguile.laptop.vista;

import javax.swing.JPanel;
import java.awt.BorderLayout;
import java.awt.LayoutManager;

import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.FlowLayout;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import javax.swing.JSpinner;
import javax.swing.SpinnerDateModel;
import org.omg.CORBA.OBJ_ADAPTER;

import com.elguile.laptop.casoDeUso.ConsultaDeVenta;
import com.elguile.laptop.entidades.Venta;

import java.util.Date;
import java.util.Calendar;
import java.util.List;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.Rectangle;
import java.awt.Dimension;

public class PanReporteVenta extends JPanel {

	private static final long serialVersionUID = 1L;
	private JTextField txtCodigo;
	private JTable table;
	private JTextField txtCantidad;
	private JTextField txtTotal;

	private final ConsultaDeVenta consultaDeVenta;

	public PanReporteVenta(ConsultaDeVenta consultaDeVenta) {
		this.consultaDeVenta = consultaDeVenta;
		initUi();
	}

	public PanReporteVenta() {
		consultaDeVenta = null;
		initUi();

	}

	public void initUi() {
		setLayout(new BorderLayout(0, 0));
		setBounds(new Rectangle(0, 0, 550, 600));

		JPanel panel = new JPanel();
		panel.setPreferredSize(new Dimension(10, 115));

		add(panel, BorderLayout.NORTH);
		panel.setLayout(null);

		JLabel label = new JLabel("Codigo");
		label.setBounds(5, 12, 65, 14);
		panel.add(label);

		txtCodigo = new JTextField();
		txtCodigo.setBounds(61, 6, 96, 25);
		txtCodigo.setColumns(10);
		panel.add(txtCodigo);

		JLabel lblInicio = new JLabel("Inicio");
		lblInicio.setBounds(61, 40, 49, 14);
		panel.add(lblInicio);

		final JSpinner spnInico = new JSpinner();
		spnInico.setBounds(104, 33, 103, 28);
		spnInico.setModel(new SpinnerDateModel(new Date(1341205200000L), null,
				null, Calendar.DAY_OF_YEAR));
		panel.add(spnInico);

		JLabel lblFin = new JLabel("Fin");
		lblFin.setBounds(219, 39, 49, 14);
		panel.add(lblFin);

		final JSpinner spnFin = new JSpinner();
		spnFin.setBounds(249, 33, 103, 25);
		spnFin.setModel(new SpinnerDateModel(new Date(1341205200000L), null,
				null, Calendar.DAY_OF_YEAR));
		panel.add(spnFin);

		JButton btnBuscar = new JButton("buscar");
		btnBuscar.setBounds(5, 79, 90, 25);
		panel.add(btnBuscar);

		JLabel lblNewLabel_1 = new JLabel("Fecha");
		lblNewLabel_1.setBounds(5, 40, 46, 14);
		panel.add(lblNewLabel_1);

		JScrollPane scrollPane = new JScrollPane();
		add(scrollPane, BorderLayout.CENTER);

		table = new JTable();
		final DefaultTableModel dataModel = new DefaultTableModel(new String[] {
				"Codigo", "fecha", "total" }, 0);
		table.setModel(dataModel);
		scrollPane.setViewportView(table);

		JPanel panel_1 = new JPanel();
		FlowLayout flowLayout_1 = (FlowLayout) panel_1.getLayout();
		flowLayout_1.setAlignment(FlowLayout.RIGHT);
		add(panel_1, BorderLayout.SOUTH);

		JLabel lblTotal = new JLabel("Total");
		panel_1.add(lblTotal);

		txtTotal = new JTextField();
		txtTotal.setEnabled(false);
		txtTotal.setText("0");
		panel_1.add(txtTotal);
		txtTotal.setColumns(10);

		JLabel lblNewLabel = new JLabel("Cantidad");
		panel_1.add(lblNewLabel);

		txtCantidad = new JTextField();
		txtCantidad.setEnabled(false);
		panel_1.add(txtCantidad);
		txtCantidad.setColumns(10);
		btnBuscar.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				List<Venta> ventas = consultaDeVenta.buscar(
						txtCodigo.getText(), (Date) spnInico.getValue(),
						(Date) spnFin.getValue());
				dataModel.setRowCount(0);
				for (Venta venta : ventas) {
					dataModel.addRow(new Object[] { venta.getCodigoVenta(),
							venta.getFecha(), venta.obtnerTotal() });
				}
				txtCantidad.setText("" + ventas.size());
				txtTotal.setText("" + consultaDeVenta.getTotal());
			}
		});
	}
}
